package dao;

import org.apache.ibatis.annotations.*;
import pojo.Pl;

import java.util.List;

@Mapper
/**
 * 评论表数据访问接口
 */
public interface PlMapper {
    // 根据id查询评论
    @Select("SELECT * FROM pl WHERE id = #{id}")
    Pl findPlById(Integer id);

    // 查询急救知识的评论
    @Select("SELECT " +
            "pl.*, yh.username " +
            "FROM pl " +
            "JOIN yh ON pl.yh_id = yh.id " +
            "WHERE pl.jjzs_id = #{jjzsId} " +
            "ORDER BY pl.create_time DESC")
    List<Pl> findByJjzsId(Integer jjzsId);

    // 查询文章的评论
    @Select("SELECT " +
            "pl.*, yh.username " +
            "FROM pl " +
            "JOIN yh ON pl.yh_id = yh.id " +
            "WHERE pl.wz_id = #{wzId} " +
            "ORDER BY pl.create_time DESC")
    List<Pl> findByWzId(Integer wzId);

    // 查询用户的评论
    @Select("SELECT " +
            "pl.*, COALESCE(jjzs.title, wz.title) AS relatedTitle " +
            "FROM pl " +
            "LEFT JOIN jjzs ON pl.jjzs_id = jjzs.id " +
            "LEFT JOIN wz ON pl.wz_id = wz.id " +
            "WHERE pl.yh_id = #{yhId} " +
            "ORDER BY pl.create_time DESC")
    List<Pl> findByYhId(Integer yhId);

    // 新增评论
    @Insert("INSERT INTO pl (yh_id, jjzs_id, wz_id, content) " +
            "VALUES (#{yhId}, #{jjzsId}, #{wzId}, #{content})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int add(Pl pl);

    // 更新评论
    @Update("UPDATE pl SET content = #{content} WHERE id = #{id}")
    int update(Pl pl);

    // 删除评论
    @Delete("DELETE FROM pl WHERE id = #{id}")
    int deleteById(Integer id);

    // 删除急救知识的评论
    @Delete("DELETE FROM pl WHERE jjzs_id = #{jjzsId}")
    int deleteByJjzsId(Integer jjzsId);

    // 删除文章的评论
    @Delete("DELETE FROM pl WHERE wz_id = #{wzId}")
    int deleteByWzId(Integer wzId);

    // 删除用户的评论
    @Delete("DELETE FROM pl WHERE yh_id = #{yhId}")
    int deleteByYhId(Integer yhId);
}